package com.aspire.aimini.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * OpenAPI配置类
 *
 * @author wangguiyou
 * @date 2024-12-19
 * @description 配置Swagger文档信息和安全认证
 */
@Configuration
public class OpenApiConfig {

        private static final String SECURITY_SCHEME_NAME = "BearerAuth";

        /**
         * 配置OpenAPI文档信息
         *
         * @return OpenAPI
         */
        @Bean
        public OpenAPI customOpenAPI() {
                return new OpenAPI()
                                .info(apiInfo())
                                .addSecurityItem(new SecurityRequirement().addList(SECURITY_SCHEME_NAME))
                                .components(new Components()
                                                .addSecuritySchemes(SECURITY_SCHEME_NAME, securityScheme()));
        }

        /**
         * API信息
         *
         * @return Info
         */
        private Info apiInfo() {
                return new Info()
                                .title("AI图形管理系统 API")
                                .description("AI图形管理系统后端接口文档")
                                .version("1.0.0")
                                .contact(new Contact()
                                                .name("wangguiyou")
                                                .email("wangguiyou@aspire.com")
                                                .url("https://github.com/wangguiyou"))
                                .license(new License()
                                                .name("MIT License")
                                                .url("https://opensource.org/licenses/MIT"));
        }

        /**
         * 安全认证方案
         *
         * @return SecurityScheme
         */
        private SecurityScheme securityScheme() {
                return new SecurityScheme()
                                .type(SecurityScheme.Type.HTTP)
                                .scheme("bearer")
                                .bearerFormat("JWT")
                                .description("请输入JWT Token，格式：Bearer {token}");
        }
}